package com.jcommander.gui.locale;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JMenu;

public class ResourceBundler {
		
	private static ResourceBundler RES_BUDLER;
	private final Context ctx;
	private final Map<String, JButton> buttons = new HashMap<String, JButton>();
	private final Map<String, JMenu> menus = new HashMap<String, JMenu>();
	
	
	public static ResourceBundler getInstance() {
		if (RES_BUDLER == null) {
			RES_BUDLER = new ResourceBundler();
		}
		return RES_BUDLER;
	}
	
	private ResourceBundler() {
		ctx = new Context("locale");
		ctx.addContextChangeListener(new ContextChangeListener() {

			public void contextChanged() {
				for (String key : buttons.keySet()) {
					buttons.get(key).setText(ctx.getBundle().getString(key));
				}
				for (String key : menus.keySet()) {
					menus.get(key).setText(ctx.getBundle().getString(key));
				}
			}
		});
	}
	
	public void setLocale(Locale locale) {
		 ctx.setLocale(locale);
	}
	
	public void addJButton(String key, JButton button) {
		buttons.put(key, button);
	}
	
	public void addJMenu(String key, JMenu menu) {
		menus.put(key, menu);
	}
}

 